package cn.com.homeyoda.aimate.aihealth.ui.message;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.base.BaseFragment;
import cn.com.homeyoda.aimate.aihealth.bean.MessageBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageListBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTitleBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityMessageBinding;
import cn.com.homeyoda.aimate.aihealth.databinding.FragmentMessageBinding;
import cn.com.homeyoda.aimate.aihealth.ui.health.DietPlanActivity;
import cn.com.homeyoda.aimate.aihealth.ui.my.MyActivity;

public class MessageFragment extends BaseFragment<FragmentMessageBinding,MessageModel> {

    private MessageTypeAdapter messageTypeAdapter;
    private MessageTypeListBean messageTypeListBean = new MessageTypeListBean();


    private MessageAdapter messageAdapter;
    private MessageListBean messageListBean = new MessageListBean();

    private int messagePos = 0;

    @Override
    public MessageModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MessageModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.messageTitleData();
        viewModel.messageData();//全部
        initRecyclerView();
    }

    private void initRecyclerView() {
        messageTypeAdapter = new MessageTypeAdapter(R.layout.message_title_item,messageTypeListBean.getData(),getContext());
        binding.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        binding.setAdapter(messageTypeAdapter);

        messageAdapter = new MessageAdapter(R.layout.message_item,messageListBean.getRows(),getContext());
        binding.setLayoutManager2(new LinearLayoutManager(getContext()));
        binding.setAdapter2(messageAdapter);

        messageTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String name = messageTypeListBean.getData().get(position).getTypeName();
                Log.i("haha",messageTypeListBean.getData().get(position).getTypeName());
                messageTypeAdapter.setPos(position);
                switchMessage(position,name);
                //viewModel.messageData(position);
            }
        });

        messageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                messagePos = position;
                Log.i("haha","messagePos===="+messagePos);
                Log.i("haha","messageAdapter===="+messageAdapter.getData().get(messagePos).getType());
                String type = messageAdapter.getData().get(messagePos).getType();
                if(type.equals("健康干预")){
                    // 饮食计划
                    startActivity(new Intent(getContext(), DietPlanActivity.class));
                }else{
                    startActivity(new Intent(getContext(), MyActivity.class));
                }
                viewModel.readMessage(messageAdapter.getData().get(messagePos).getId());
            }
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void switchMessage(int pos, String typeName){
        if(pos == 0){
            messageAdapter.setNewData(messageListBean.getRows());
        }else{
            if(messageListBean.getRows() != null){
                List<MessageListBean.RowsBean> result = messageListBean.getRows().stream()
                        .filter(p -> p.getType().equals(typeName))
                        .collect(Collectors.toList());
                messageAdapter.setNewData(result);
            }

        }

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.stateLiveData.observe(this,messageTypeListBeans -> {
            messageTypeListBean = messageTypeListBeans;
            messageTypeAdapter.setNewData(messageTypeListBean.getData());
        });

        viewModel.messageData.observe(this,messageListBeans -> {
            messageListBean = messageListBeans;
            if(messageListBean.getRows() == null || messageListBean.getRows().size()==0){
                binding.tip.setVisibility(View.VISIBLE);
                Log.i("haha","VISIBLE");
            }else{
                Log.i("haha","GONE");
                binding.tip.setVisibility(View.GONE);
            }
            messageAdapter.setNewData(messageListBean.getRows());
        });

        viewModel.readMsgData.observe(this,baseResponse2 -> {
           if(baseResponse2.getCode()==200){
               if(messageAdapter.getData().get(messagePos).getType().equals("调查问卷")){

                   Log.i("haha","调查问卷===已读");
               }else if(messageAdapter.getData().get(messagePos).getType().equals("健康干预")){
                   Log.i("haha","健康干预===已读");
               }else if(messageAdapter.getData().get(messagePos).getType().equals("检测")){
                   Log.i("haha","检测===已读");
               }
           }
        });
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_message;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
